package com.jason.controller;

import com.jason.constant.ResponseConstant;
import com.jason.entity.User;
import com.jason.service.UserService;
import com.jason.vo.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/user/register")
    public Response register(@RequestParam("email") String email,
                             @RequestParam("password") String password,
                             @RequestParam("confirmPassword") String confirmPassword,
                             @RequestParam("code") String code) {
        userService.register(email, password, confirmPassword, code);
        return new Response(ResponseConstant.SUCCESS);
    }

    @RequestMapping("/user/login")
    public Response login(@RequestParam("email") String email,
                          @RequestParam("password") String password,
                          HttpServletResponse response) {
        userService.login(email, password, response);
        return new Response(ResponseConstant.SUCCESS);
    }

    @RequestMapping("/user/getUserInfo")
    public Response<User> getUserInfo(HttpServletRequest request) {
        Response response = new Response();
        response.setCode(ResponseConstant.SUCCESS);
        response.setData(userService.getUserInfo(request));
        return response;
    }
}
